<?php

namespace App\Http\Controllers\background;

use App\Models\Mysql\XnMechantOrderInvocingRelation;
use App\Models\Mysql\XnOrder;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Mysql\XnMechantOrderInvoicing;

class InvoiceController extends Controller
{
    public function examineList(Request $request){
        $mechant_number = $request->post('merchantNum','');
        $mechant_name = $request->post('mechant_name','');
        $inc_status = $request->post('inc_status','');
        $condition = [];
        if(!empty($mechant_number)){
            $condition[] = ['mechant_number','=',$mechant_number];
        }
        if(!empty($mechant_name)){
            $condition[] = ['mechant_name','like','%'.$mechant_name.'%'];
        }
        if(!empty($inc_status)){
            $condition[] = ['invoicing_status','=',$inc_status];
        }
        $list = XnMechantOrderInvoicing::where($condition)
            -> orderBy('time','desc')
            -> paginate(10)
            -> toArray();
        return successly($list);
    }

    public function examine(Request $request){
        $id = $request -> post('inc_id');
        $res = XnMechantOrderInvoicing::where('id',$id) -> update(['invoicing_status' => 2]);
        if($res){
            return successly();
        }else{
            return fails('审核失败');
        }
    }

    public function InvoiceOrderList(Request $request){
        $id = $request-> post('ids');
        $goods_name = $request->post('goods_name','');
        $order_no = $request->post('order_no','');
        $condition = [];
        if(!empty($goods_name)){
            $condition[] = ['goods_name','like','%'.$goods_name.'%'];
        }
        if(!empty($order_no)){
            $condition[] = ['order_sn','=',$order_no];
        }
        $order_info = XnMechantOrderInvocingRelation::where('in_id',$id) -> pluck('o_id');
        $list = XnOrder::whereIn('id',$order_info)
            -> where($condition)
            -> orderBy('recharge_time','desc')
            -> paginate(10)
            -> toArray();
        return successly($list);
    }

    public function InvoiceMoney(Request $request){
        $id = $request->post('ids');
        $goods_name =$request->post('goods_name', '');
        $order_no = $request->post('order_no', '');
        $order_info = XnMechantOrderInvocingRelation :: where('in_id',$id) -> pluck('o_id');
        $condition = [];
        if(!empty($goods_name)){
            $condition[] = ['goods_name','like','%'.$goods_name.'%'];
        }
        if(!empty($order_no)){
            $condition[] = ['order_sn','=',$order_no];
        }
        $orderinfos = XnOrder::whereIn('id',$order_info)
            -> where($condition)
            -> selectRaw('count(id) as all_count,sum(all_money) as all_money')
            -> first()
            -> toArray();
        $list['order_num'] = $orderinfos['all_count'];
        $list['order_money'] = $orderinfos['all_money'];
        return successly($list);
    }
}
